package bean;

import bean.Account;
import parts.JDBC;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

//用于加载数据库中的数据
public class load {
    //必须有异常处理，否则会报错
    //Connection con=new Link().create();
    Statement sql;//处理语句对象
    ResultSet rs;//结果集对象
    String SQL;

    // ArrayList<Object> ar = new ArrayList<Object>();
    //查询表，对所有的表通用，需要结果集，达到信息匹配的目的
    public ArrayList<Account> loadact(String SQL){
        Connection con = new JDBC().create();
        ArrayList<Account> ar =new  ArrayList<Account>();
        try{
            sql=con.createStatement();
            rs=sql.executeQuery(SQL);
            while(rs.next()){
                Account ac = new Account(rs.getString(1), rs.getString(2),rs.getString(3),rs.getString(4),
                        rs.getString(5),rs.getString(6),rs.getString(7));
                ar.add(ac);
            }
            con.close();//关闭数据库
        }catch(Exception e){
            System.out.print(e);
        }finally{
            try{
                con.close();
            }catch(Exception e){

            }
        }
        return ar;
    }
    //加载科室信息
    public ArrayList<dept> loaddept(String SQL){
        Connection con = new JDBC().create();
        ArrayList<dept> ar =new  ArrayList<dept>();
        try{
            sql=con.createStatement();
            rs=sql.executeQuery(SQL);
            while(rs.next()){
                dept ac = new dept(rs.getString(1), rs.getString(2),rs.getString(3),rs.getString(4),
                        rs.getString(5),rs.getString(6));
                ar.add(ac);
            }
            con.close();//关闭数据库
        }catch(Exception e){
            System.out.print(e);
        }finally{
            try{
                con.close();
            }catch(Exception e){

            }
        }
        return ar;
    }

    public ArrayList<doc> loaddoc(String SQL){
        Connection con = new JDBC().create();
        ArrayList<doc> ar =new  ArrayList<doc>();
        try{
            sql=con.createStatement();
            rs=sql.executeQuery(SQL);
            while(rs.next()){
                doc ac = new doc(rs.getString(1), rs.getString(2),rs.getString(3),rs.getString(4),
                        rs.getString(5),rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9));
                ar.add(ac);
            }
            con.close();//关闭数据库
        }catch(Exception e){
            System.out.print(e);
        }finally{
            try{
                con.close();
            }catch(Exception e){

            }
        }
        return ar;
    }

    public ArrayList<record> loadrecord(String SQL){
        Connection con = new JDBC().create();
        ArrayList<record> ar =new  ArrayList<record>();
        try{
            sql=con.createStatement();
            rs=sql.executeQuery(SQL);
            while(rs.next()){
                record ac = new record(rs.getString(1), rs.getString(2),rs.getString(3),rs.getString(4));
                ar.add(ac);
            }
            con.close();//关闭数据库
        }catch(Exception e){
            System.out.print(e);
        }finally{
            try{
                con.close();
            }catch(Exception e){

            }
        }
        return ar;
    }
    //目的是，实现ResultSet到Result的转换，目前尚未解决
    public ResultSet load(String SQL){
        Connection con = new JDBC().create();
        try{
            sql=con.createStatement();
            rs=sql.executeQuery(SQL);
            //Result result = ResultSupport.toResult(rs);
            con.close();//关闭数据库
        }catch(Exception e){
            System.out.print(e);
        } finally{
            try{
                con.close();
            }catch(Exception e){

            }
        }
        return rs;
    }
    //删除，修改，增加，无需结果集，只需要执行SQL
    public void opert(String SQL){
        Connection con = new JDBC().create();
        try{
            sql=con.createStatement();
            sql.execute(SQL);
            System.out.println("SQL语句执行成功");
            con.close();//关闭数据库
        }catch(Exception e){
            System.out.print(e);
        }finally{
            try{
                con.close();
            }catch(Exception e){

            }
        }
    }


}
